<?php

function view_notes($config,$param) {
    view_header_up($config,$param);
    //
    ?>
    <link  href="http://fonts.googleapis.com/css?family=Reenie+Beanie:regular" rel="stylesheet" type="text/css"> 
    <title>Idebeda-Mading</title>
    <?
    //
    view_header_bottom($config,$param);
    $off = isset($_GET['off']) ? ((intval($_GET['off']) > 0) ? intval($_GET['off']) : 1) : 1;
    ?>
    <h2>Papan Mading</h2>
    <?php
    if (isset($_SESSION['addsucc'])) {
        echo '<div class="success alert">'.$_SESSION['addsucc'].'</div>';
        unset($_SESSION['addsucc']);
    }
    ?>
    <form action="" method="post" onsubmit="return validateForm(this)">
		<h5><a href="javascript:showhide('tambah-notes');">Pasang Pesan</a></h5>
		<div id="tambah-notes" style="display:none">
			<ul>
			<?php
				$db = new db($config);
				$dari = $_SESSION['logged']['id'];
				$orangs = $db->query("SELECT * FROM mn_crew WHERE 1 ORDER BY idCrew ASC");
				foreach ($orangs as $orang){
					$idc = $orang['idCrew'];
					$uname = $orang['username'];
					echo "<div style=\"float:left\"><input name=\"ckbox[]\" id=\"check$idc\" value=\"$idc\" type=\"checkbox\"><span></span> $uname </div>";
				}
				echo "<div style=\"clear:both\"></div>";
			?>
				<li class="field">
					<label class="inline" for="pesan">Isi Pesan</label>
					<textarea class="input textarea" rows="3" placeholder="isi pesan" name="pesan"></textarea>
				</li>
				<div class="medium primary btn"><input type="submit" name="pasang" value="Pasang" /></div>
			</ul>
		</div>
		<h5><a href="<? echo $config['home_url']."mading/dari"; ?>">Mading OUT</a></h5>
		<h5><a href="javascript:showhide('isi-notes');">Mading IN</a></h5>
	</form>
	<div id="isi-notes">
			<?
			$db = new db($config);
			$pesans = $db->query("SELECT * FROM mn_notes WHERE ke = '0' OR ke = '".$_SESSION['logged']['id']."' ORDER BY tgl DESC");
			$delim = 3;
			$total = count($pesans);
			$maxpage = $total / $delim;
			$maxpage = ($maxpage < 1) ? 1 : $maxpage;
			$offset = ($off-1)*$delim;
			$pesans = $db->query("SELECT * FROM mn_notes WHERE ke = '0' OR ke = '".$_SESSION['logged']['id']."' ORDER BY tgl DESC LIMIT $offset , $delim");
			if (count($pesans)>0){
				echo "<ul class=\"notes\">";
				foreach($pesans as $pesan){
					$tgl = helper::formDate($pesan['tgl']);
					$pengirim = $pesan['dari'];
					$pengirim = $db->query("SELECT username FROM mn_crew WHERE idCrew = '$pengirim'");
					$pengirim = $pengirim[0]['username'];
				?>
				<li>
				  <div>
					<h2><? echo "#".$pesan['idNotes']." : ".$tgl; ?></h2>
					<p><?php
							$msg = $pesan['isi'];
							$msg = str_replace("\n", "<br />", $msg);
							echo $msg;
						?>
					</p>
					<p style="text-align:right">~<? echo $pengirim; ?></p>
				  </div>
				</li>
		<?
				}
				echo "</ul>";
				if ($off>1)
					echo "<div style=\"float:left; margin-bottom:50px\"><a href=\"".$config['home_url']."mading?off=".($off-1)."\">&lt;&lt;Balik</a></div>";
				if ($off < $maxpage)
					echo "<div style=\"float:right; margin-bottom:50px\"><a href=\"".$config['home_url']."mading?off=".($off+1)."\">Lanjut&gt;&gt;</a></div>";
				echo "<div style=\"clear:both\"></div>";
			}else{
				echo "<h2>Tidak ada pengumuman</h2>";
			}
		?>
	</div>
	<script type="text/javascript">
		function showhide(id){
			$('#'+id).toggle('slow');
		}
		function validateEmpty(fld) {
			var error = "";
		 
			if (fld.value.length == 0) {
				fld.style.background = 'Yellow'; 
				error = "1";
			} else {
				fld.style.background = 'White';
			}
			return error;  
		}
		function validateForm(theForm) {
			var reason = "";
			
			  reason += validateEmpty(theForm.pesan);
				  
			  if (reason != "") {
				alert("Perhatian, isi yang masih kosong.");
				return false;
			  }

			  return true;
			}
	</script>
<?
	view_footer($config,$param);
}

function view_notesb($config,$param) {
    view_header_up($config,$param);
    //
    ?>
    <link  href="http://fonts.googleapis.com/css?family=Reenie+Beanie:regular" rel="stylesheet" type="text/css"> 
    <title>Idebeda-Mading</title>
    <?
    //
    view_header_bottom($config,$param);
    $db = new db($config);
    $idn = $config['ruri'][1];
	$que = "SELECT * FROM mn_notes WHERE dari = '".$_SESSION['logged']['id']."' AND idNotes = '$idn'";
	$pesans = $db->query($que);
    ?>
    <h2>Ubah Mading</h2>
    <form action="" method="post" onsubmit="return validateForm(this)">
		<div id="tambah-notes" style="display:block">
			<ul>
				<li class="field">
					<label class="inline" for="pesan">Isi Pesan</label>
					<input type="hidden" name="idmading" value="<?php echo $idn; ?>" />
					<textarea class="input textarea" rows="3" placeholder="isi pesan" name="pesan"><?php echo $pesans[0]['isi']; ?></textarea>
				</li>
				<div class="medium primary btn"><input type="submit" name="ulang" value="Ubah" /></div>
			</ul>
		</div>
	</form>
	<script type="text/javascript">
		function showhide(id){
			$('#'+id).toggle('slow');
		}
		function validateEmpty(fld) {
			var error = "";
		 
			if (fld.value.length == 0) {
				fld.style.background = 'Yellow'; 
				error = "1";
			} else {
				fld.style.background = 'White';
			}
			return error;  
		}
		function validateForm(theForm) {
			var reason = "";
			
			  reason += validateEmpty(theForm.pesan);
				  
			  if (reason != "") {
				alert("Perhatian, isi yang masih kosong.");
				return false;
			  }

			  return true;
			}
	</script>
<?
	view_footer($config,$param);
}

function view_notesd($config,$param) {
    view_header_up($config,$param);
    //
    ?>
    <link  href="http://fonts.googleapis.com/css?family=Reenie+Beanie:regular" rel="stylesheet" type="text/css"> 
    <title>Idebeda-Mading</title>
    <?
    //
    view_header_bottom($config,$param);
    $off = isset($_GET['off']) ? ((intval($_GET['off']) > 0) ? intval($_GET['off']) : 1) : 1;
    ?>
    <h2>Mading Sendiri</h2>
    <?php
    if (isset($_SESSION['editsucc'])) {
        echo '<div class="success alert">'.$_SESSION['editsucc'].'</div>';
        unset($_SESSION['editsucc']);
    }
    ?>
		<?
		$db = new db($config);
		$pesans = $db->query("SELECT * FROM mn_notes WHERE dari = '".$_SESSION['logged']['id']."' ORDER BY tgl DESC");
		$total = count($pesans);
		$delim = 3;
		$maxpage = $total / $delim;
		$maxpage = ($maxpage < 1) ? 1 : $maxpage;
		$offset = ($off-1)*$delim;
		$pesans = $db->query("SELECT * FROM mn_notes WHERE dari = '".$_SESSION['logged']['id']."' ORDER BY tgl DESC LIMIT $offset , $delim");
		if (count($pesans)>0){
			echo "<ul class=\"notes\">";
			foreach($pesans as $pesan){
				$tgl = helper::formDate($pesan['tgl']);
				$pengirim = $pesan['dari'];
				$pengirim = $db->query("SELECT username FROM mn_crew WHERE idCrew = '$pengirim'");
				$pengirim = $pengirim[0]['username'];
				$penerima = $pesan['ke'];
				$penerima = $db->query("SELECT username FROM mn_crew WHERE idCrew = '$penerima'");
				$penerima = $penerima[0]['username'];
			?>
			<li>
			  <div>
				<h2><? echo "#".$pesan['idNotes']." : ".$tgl; ?></h2>
				<a href="<? echo $config['home_url']."mading/".$pesan['idNotes']."/hapus"; ?>"><i class="icon-cancel"></i></a> <a href="<? echo $config['home_url']."mading/".$pesan['idNotes']."/ubah"; ?>"><i class="icon-pencil"></i></a>
				<p style="text-align:right">kepada <? echo $penerima; ?></p>
				<p><?php
						$msg = $pesan['isi'];
						$msg = str_replace("\n", "<br />", $msg);
						echo $msg;
					?>
				</p>
			  </div>
			</li>
	<?
			}
			echo "</ul>";
			if ($off>1)
				echo "<div style=\"float:left; margin-bottom:50px\"><a href=\"".$config['home_url']."mading/dari?off=".($off-1)."\">&lt;&lt;Balik</a></div>";
			if ($off < $maxpage)
				echo "<div style=\"float:right; margin-bottom:50px\"><a href=\"".$config['home_url']."mading/dari?off=".($off+1)."\">Lanjut&gt;&gt;</a></div>";
			echo "<div style=\"clear:both\"></div>";
		}else{
			echo "<h2>Tidak ada pengumuman</h2>";
		}
	?>
<?
	view_footer($config,$param);
}
